package com.qf.shopping.service.carService.impl;

import com.qf.shopping.mapper.carMapper.CarMakeMapper;
import com.qf.shopping.mapper.carMapper.CarMapper;
import com.qf.shopping.mapper.carMapper.CarModelImageMapper;
import com.qf.shopping.pojo.car.CarMake;
import com.qf.shopping.pojo.car.CarModelImage;
import com.qf.shopping.service.carService.ICarService;
import com.qf.shopping.vo.carVO.CarConstom;
import com.qf.shopping.vo.carVO.CarVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Humy
 * @date 2019/4/17 11:28
 */

@Service
@Transactional
public class CarServiceImpl implements ICarService {

    @Autowired
    private CarMapper carMapper = null;

    @Autowired
    private CarMakeMapper carMakeMapper = null;

    @Autowired
    private CarModelImageMapper carModelImageMapper = null;


    /**
     * 新增车款
     * @param carVo
     */
    @Override
    public void addCar(CarVO carVo) {

        CarConstom carConstom = carVo.getCarConstom();

        //通过品牌id获取汽车品牌对象
        CarMake carMake = carMakeMapper.findCarMakeByName(carConstom.getMakeName());
        System.out.println(carMake+"+++++++++++++++++++++");
//        carConstom.setBrandId(carMake.getBrandId());
//        carConstom.setBrandName(carMake.getBrandName());
//        carConstom.setCountryId(carMake.getCountryId());

        //获取车系名字
        String modelName = carConstom.getModelName();
        //根据车系id获取汽车车型图片对象，最终获取年款属性
//        CarModelImage carModelImage = carModelImageMapper.findCarModelImageByModelName(carConstom.getModelName());
//        carConstom.setYear(carModelImage.getYear());

        //生成创建时间
        Date date1 = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        String s = format.format(date1);
        Long date = Long.valueOf(s);
        carConstom.setCreateTime(date);

        carVo.setCarConstom(carConstom);
        carMapper.insert(carVo);

    }

    /**
     * 查询所有车款
     * @return
     */
    @Override
    public List<CarVO> queryAllCars() {

        List<CarVO> cars = carMapper.findAllCars();
        System.out.println("carService-------------"+cars);
        return cars;
    }




    /**
     * 查询车款
     * @param carVo
     * @return
     */
    @Override
    public List<CarVO> queryCars(CarVO carVo) {

        List<CarVO> carVos = carMapper.findCarByVo(carVo);

        return carVos;
    }

    /**
     * 根据id查询车款
     * @param id
     * @return
     */
    @Override
    public CarVO queryCar(Long id) {

        CarVO carVo = carMapper.findCarById(id);

        return carVo;
    }

    @Override
    public void updateCar(CarVO carVo) {

        carMapper.updateCar(carVo);

    }

    @Override
    public void deleteCar(Long...ids) {

        carMapper.deleteCar(ids);

    }
}
